/*
 * @# AlbumRedis.java 2011-12-26 下午04:40:27
 * 
 * Copyright (C) 2010 - 2011 广州羿安信息科技有限公司
 * Guangzhou ianswer information technology co. ltd.
 * 
 * All rights reserved!
 */
package com.ianswer.px.album.redis;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.collections.MapUtils;

import com.ianswer.px.album.entity.Photo;
import com.ianswer.px.common.redis.RedisSupport;

/**
 * 相册在redis中的处理.
 * 
 * @author alvin hwang
 */
public class PhotoRedis extends RedisSupport {

	private static final String ALBUM_KEY = "photo:";
	private static final String USERID = "userid";
	private static final String ADDTIME = "addtime";
	private static final String FAVORS = "favors";
	private static final String LIKES = "likes";
	private static final String VISITS = "visits";
	private static final String TRANSFERRED = "transferred";
	private static final String COMMENTS_TOTAL = "commentstotal";
	private static final String COMMENTS = "comments";

	public static void init(Photo photo) {
		Map<String, String> photoCache = new HashMap<String, String>();
		photoCache.put(ADDTIME, photo.getCreatedAt().toString());
		photoCache.put(USERID, photo.getUserId().toString());
		photoCache.put(FAVORS, "0");
		photoCache.put(LIKES, "0");
		photoCache.put(VISITS, "0");
		photoCache.put(TRANSFERRED, "0");
		photoCache.put(COMMENTS_TOTAL, "0");
		photoCache.put(COMMENTS, "0");

		redis().hmset(key(photo.getId()), photoCache);
	}

	public static boolean delete(Long photoId) {
		return redis().del(key(photoId)) > 0;
	}
	
	public static Photo get(Long photoId) {
		Map<String, String> photoCache = redis().hgetAll(key(photoId));
		if (MapUtils.isEmpty(photoCache)) {
			return null;
		}
		Photo photo = new Photo();
		photo.setId(photoId);
		photo.setCreatedAt(getLong(photoCache, ADDTIME));
		photo.setFavors(getInt(photoCache, FAVORS));
		photo.setLikes(getInt(photoCache, LIKES));
		photo.setVisits(getInt(photoCache, VISITS));
		photo.setTransferred(getInt(photoCache, TRANSFERRED));
		photo.setCommentsTotal(getInt(photoCache, COMMENTS_TOTAL));
		photo.setComments(getInt(photoCache, COMMENTS));

		return photo;
	}

	public static int likes(Long photoId) {
		return getInt(key(photoId), LIKES);
	}
	
	public static int favors(Long photoId) {
		return getInt(key(photoId), FAVORS);
	}
	
	public static int transferred(Long photoId) {
		return getInt(key(photoId), TRANSFERRED);
	}
	
	public static int commentsTotal(Long photoId) {
		return getInt(key(photoId), COMMENTS_TOTAL);
	}

	public static void incrFavors(Long photoId) {
		redis().hincrBy(key(photoId), FAVORS, 1);
	}

	public static void decrFavors(Long photoId) {
		redis().hincrBy(key(photoId), FAVORS, -1);
	}

	public static void incrLikes(Long photoId) {
		redis().hincrBy(key(photoId), LIKES, 1);
	}

	public static void decrLikes(Long photoId) {
		redis().hincrBy(key(photoId), LIKES, -1);
	}

	public static void incrVisits(Long photoId) {
		redis().hincrBy(key(photoId), VISITS, 1);
	}

	public static void decrVisits(Long photoId) {
		redis().hincrBy(key(photoId), VISITS, -1);
	}

	public static void incrTransferred(Long photoId) {
		redis().hincrBy(key(photoId), TRANSFERRED, 1);
	}

	public static void decrTransferred(Long photoId) {
		redis().hincrBy(key(photoId), TRANSFERRED, -1);
	}

	public static void incrCommentsTotal(Long photoId) {
		redis().hincrBy(key(photoId), COMMENTS_TOTAL, 1);
	}

	public static void incrComments(Long photoId) {
		redis().hincrBy(key(photoId), COMMENTS, 1);
	}

	public static void decrComments(Long photoId) {
		redis().hincrBy(key(photoId), COMMENTS, -1);
	}

	private static String key(Long photoId) {
		return ALBUM_KEY + photoId;
	}

	public static void makeup(Photo photo) {
		Photo cache = get(photo.getId());
		if (cache == null) {
			return;
		}
		photo.setComments(cache.getComments());
		photo.setFavors(cache.getFavors());
		photo.setLikes(cache.getLikes());
		photo.setTransferred(cache.getTransferred());
		photo.setVisits(cache.getVisits());
	}
}
